using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Resources;
using System.Reflection;
using System.Windows.Forms;

namespace Microsoft.Samples.BizTalk.Adapter.Tcp.Setup.CustomActions.Forms
{
    /// <summary>
    ///     This form shows a file that has been passed by the installer.
    /// </summary>
    public partial class FileDialog : Form
    {
        #region Private Instance Members

        private string          _headerText;// The text to display
        private string          _filePath;  // The file to load
        private ResourceManager _resMgr;    // Resource manager used to access assembly resources

        #endregion

        #region Constructor

        /// <summary>
        ///     This is the default constructor for the class.
        /// </summary>
        public FileDialog()
        {
            InitializeComponent();

            // Initialize resource manager
            this._resMgr = new ResourceManager(ResourceConstants.BaseName, Assembly.GetExecutingAssembly());
        }

        #endregion

        #region Public Instance Properties

        /// <summary>
        ///     This property gets or sets the text that the dialog is to display.
        /// </summary>
        public string HeaderText
        {
            get { return (this._headerText); }
            set { this._headerText = value; }
        }

        /// <summary>
        ///     This property gets or sets the file path of the file for the dialog to display.
        /// </summary>
        public string FilePath
        {
            get { return (this._filePath); }
            set { this._filePath = value; }
        }

        #endregion

        #region Private Instance Methods

        /// <summary>
        ///     This method loads the file from disk into the textbox.
        /// </summary>
        private void LoadFileIntoTextBox(RichTextBox textBox)
        {
            using (StreamReader reader = new StreamReader(this.FilePath, true))
            {
                try
                {
                    // Load file
                    textBox.Text = reader.ReadToEnd();
                }
                finally
                {
                    if (reader != null) reader.Close();
                }
            }
        }

        #endregion

        #region Private Instance Handlers

        /// <summary>
        ///     This event handler loads the form.
        /// </summary>
        private void FileDialog_Load(object sender, EventArgs e)
        {
            this.labelHeader.Text = this.HeaderText;
            this.LoadFileIntoTextBox(this.richTextBox);
        }

        /// <summary>
        ///     This event handler is fired whenever the button is clicked.
        /// </summary>
        private void buttonNext_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        #endregion
    }
}